package com.javasm.service.impl;

import com.javasm.dao.SkillGameDao;
import com.javasm.dao.SkillGameLevelDao;
import com.javasm.dao.SkillGameServerDao;
import com.javasm.entity.SkillGame;
import com.javasm.entity.SkillGameLevel;
import com.javasm.entity.SkillGameServer;
import com.javasm.service.SkillGameService;
import com.javasm.utils.MyBatisHeapler;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 技能所属游戏(CustomerSkillGame)表服务实现类
 *
 * @author makejava
 * @since 2024-08-21 16:28:23
 */

public class SkillGameServiceImpl implements SkillGameService {

    @Override
    public List<SkillGame> listAllGame(SkillGame inputGame, Integer page, Integer pageSize) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();

        SkillGameDao gameDao = sqlSession.getMapper(SkillGameDao.class);
        List<SkillGame> listGame = gameDao.listAllGame(inputGame,(page-1)*pageSize,pageSize);

        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return listGame;
    }

    @Override
    public Integer countGames(SkillGame inputGame) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();

        SkillGameDao gameDao = sqlSession.getMapper(SkillGameDao.class);
        Integer total = gameDao.countGames(inputGame);

        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return total;
    }

    @Override
    public Integer addGame(SkillGame inputGame) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        SkillGameDao gameDao = sqlSession.getMapper(SkillGameDao.class);
        Integer resNum = gameDao.addGame(inputGame);

        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }

    @Override
    public SkillGame getGameById(Integer gamdId) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        SkillGameDao gameDao = sqlSession.getMapper(SkillGameDao.class);
        SkillGame editGame = gameDao.getGameById(gamdId);

        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return editGame;
    }

    @Override
    public Integer editGame(SkillGame inputGame) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        SkillGameDao gameDao = sqlSession.getMapper(SkillGameDao.class);
        Integer resNum = gameDao.editGame(inputGame);

        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }

    @Override
    public Integer deleteGameById(Integer gamdId) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        SkillGameDao gameDao = sqlSession.getMapper(SkillGameDao.class);
        Integer resNum = gameDao.deleteGameById(gamdId);

        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }

    @Override
    public boolean deleteCheck(Integer gamdId) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        SkillGameLevelDao gameLevelDao = sqlSession.getMapper(SkillGameLevelDao.class);
        SkillGameServerDao gameServerDao = sqlSession.getMapper(SkillGameServerDao.class);
        Integer levelNum = gameLevelDao.countGameLevels(new SkillGameLevel(gamdId));
        Integer serverNum = gameServerDao.countGameServers(new SkillGameServer(gamdId));
        System.out.println(levelNum+serverNum);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return levelNum+serverNum==0?true:false;
    }
}
